var app = getApp();
Page({

  data: {
    //推荐歌曲
    hotBgmList: [],
    //歌曲列表
    fashionBgmList: [],
    //搜索列表
    searchBmgList: [],
    //当前选择bgm
    selectBgm: null,
    //当前页
    pageNumber: 1,
    //搜索
    search: false,
    //关键字
    keyword: '',
    //删除标记
    deleteFlag: true
  },

  onLoad: function(p) {
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })

    //获取推荐音乐
    this.getRecommendMusicList();
    //获取音乐列表
    this.getMusicList();
  },
  //初始化播放器
  onReady: function() {
    this.innerAudioContext = wx.createInnerAudioContext();
  },

  onPullDownRefresh: function() {
    //获取推荐音乐
    this.getRecommendMusicList();
    //获取音乐列表
    this.getMusicList();
  },

  onReachBottom: function() {
    this.data.pageNumber = this.data.pageNumber + 1;
    //获取音乐列表
    this.getMusicList();
  },

  //销毁播放器
  onUnload: function() {
    this.innerAudioContext.destroy();
  },

  //输入内容
  searchInput: function(e) {
    var cursor = e.detail.detail.cursor;
    var value = e.detail.detail.value;
    if (cursor > 10) {
      value = value.substr(0, 10);
    }
    this.setData({
      keyword: value
    })
  },

  //清空输入内容
  clearInput: function() {
    this.setData({
      deleteFlag: true,
      keyword: '',
      search: false
    })
  },
  //搜索
  searchSubmit: function() {
    var that = this;
    //键入关键字
    var keyword = this.data.keyword;
    if (keyword == '') {
      return;
    }
    this.setData({
      deleteFlag: false,
      search: true
    })
    wx.request({
      url: app.globalData.requestUrl + 'searchMusic',
      method: 'GET',
      data: {
        userId: app.globalData.user.id,
        keyword: keyword
      },
      success: function(r) {1
        if (r.data.code == 200) {
          that.setData({
            searchBmgList: r.data.data
          })
        } else {
          wx.showToast({
            title: r.data.message,
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail() {
        wx.showToast({
          title: '搜索音乐失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //获取推荐音乐
  getRecommendMusicList: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getRecommendMusicList',
      method: 'GET',
      success: function(r) {
        if (r.data.code == 200) {
          that.setData({
            hotBgmList: r.data.data
          })
        } else {
          wx.showToast({
            title: r.data.message,
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail() {
        wx.showToast({
          title: '获取热门音乐失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //获取音乐列表
  getMusicList: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getMusicList',
      method: 'GET',
      data: {
        pageNumber: that.data.pageNumber
      },
      success: function(r) {
        if (r.data.code == 200) {
          if (r.data.data.length == 0) {
            wx.showToast({
              title: '音乐库里没有更多的音乐了Q-Q',
              icon: 'none',
              duration: 3000
            })
          } else {
            that.setData({
              fashionBgmList: that.data.fashionBgmList.concat(r.data.data)
            })
          }
        } else {
          wx.showToast({
            title: r.data.message,
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail() {
        wx.showToast({
          title: '获取热门音乐失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //选择音乐
  selBgm: function() {
    var pages = getCurrentPages();
    var page = pages[pages.length - 2];
    page.setData({
      bgm: this.data.selectBgm
    })
    this.rollback();
  },

  //音乐背景图加载失败
  imageError: function(e) {
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    if (type == 'hot') {
      var bgm = this.data.hotBgmList[index];
      bgm.poster = 'https://petbgm.oss-cn-beijing.aliyuncs.com/morentupian.jpg';
      this.setData({
        hotBgmList: this.data.hotBgmList
      })
    } else if (type == 'fashion') {
      var bgm = this.data.fashionBgmList[index];
      bgm.poster = 'https://petbgm.oss-cn-beijing.aliyuncs.com/morentupian.jpg';
      this.setData({
        fashionBgmList: this.data.fashionBgmList
      })
    } else {
      var bgm = this.data.searchBmgList[index];
      bgm.poster = 'https://petbgm.oss-cn-beijing.aliyuncs.com/morentupian.jpg';
      this.setData({
        searchBmgList: this.data.searchBmgList
      })
    }

  },
  //选择流行音乐
  selectBgm: function(e) {
    //bgm下标
    var index = e.currentTarget.dataset.index;
    //是热门的还是流行的
    var type = e.currentTarget.dataset.type;
    //切换bgm
    if (type == 'hot') {
      this.setData({
        selectBgm: this.data.hotBgmList[index],
      })
    } else if (type == 'fashion') {
      this.setData({
        selectBgm: this.data.fashionBgmList[index]
      })
    } else {
      this.setData({
        selectBgm: this.data.searchBmgList[index]
      })
    }
    //设置播放URL
    this.innerAudioContext.src = this.data.selectBgm.src;
    //自动播放
    this.innerAudioContext.autoplay = true;
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  }


})